package com.orhon.smartcampus.modules.material.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.material.dao.FloorDao;
import com.orhon.smartcampus.modules.material.dao.RoomDao;
import com.orhon.smartcampus.modules.material.entity.Floor;
import com.orhon.smartcampus.modules.material.entity.Room;
import com.orhon.smartcampus.modules.material.gqlInputModel.GQLRoomInput;
import com.orhon.smartcampus.modules.material.inputMapper.RoomInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddRoomMutation implements DataFetcher {



    @Autowired
    RoomDao roomDao;

    @Autowired
    FloorDao floorDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 房间管理
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);

        GQLRoomInput input = RoomInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Room room = null;

        if (input.getId() != null) {
            room = roomDao.get(Room.class, input.getId());
            room.setUpdatedAt(new Date());
        }
        else{
            room = new Room();
            room.setCreatedAt(new Date());
        }

        if(input.getFloor_id() != null) {
            room.setMaterialFloorByFloorId( floorDao.get(Floor.class, input.getFloor_id()));
        }
        if(input.getArea() != null){

            room.setRoomArea(input.getArea());
        }
        if(input.getCode() != null){

            room.setRoomCode(input.getCode());
        }
        if(input.getType() != null){

            room.setRoomType(input.getType());
        }
        if(input.getKye_num() != null){

            room.setKeyNum(input.getKye_num());
        }
        if(input.getStatus() != null){

            room.setRoomStatus(input.getStatus());
        }
        if(schoolId != null) {
            room.setBaseSchoolsBySchoolId( schoolsDao.get(Schools.class, schoolId));
        }
        if(input.getManager() != null){

            room.setRoomManager(JSON.toJSONString(input.getManager()));
        }
        if(input.getKeys_user_name() != null){

            room.setKeysUserName(JSON.toJSONString(input.getKeys_user_name()));
        }
        if(input.getName() != null){

            room.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getRemarks() != null){

            room.setRemarks(JSON.toJSONString(input.getRemarks()));
        }
        try{
            roomDao.saveOrUpdate(room);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

